//
//  MQMyAruthViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/10/24.
//  Copyright © 2017年 mq. All rights reserved.
//

#import "MQMyAruthViewController.h"
#import "MQAruthTitleview.h"
#import "MQMyAruthCell.h"
#import "MQBaseUserInfoModel.h"
#import "MQMyAgentListModel.h"
#import "MQAruthTableViewCell.h"
#import "MQAgentAruthViewController.h"
#import "MQmyauthTableViewCell.h"

#import "MQadviews.h"
#import "MQAdmodels.h"
#import "MQAdDetailViewController.h"
@interface MQMyAruthViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *myaruthtableview;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)MQAruthTitleview *titleview;

@property(nonatomic,assign)NSInteger currentselectindex; //0 我的授权 1待审核 2我的下级


@property(nonatomic,strong)NSMutableArray *subdataSource;

@property(nonatomic,strong)NSMutableArray *waitdatasource;


@property(nonatomic,strong)MQadviews *adviews;

@property(nonatomic,strong)MQAdmodels *admodels;


@end

@implementation MQMyAruthViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _dataSource = [NSMutableArray array];
    _waitdatasource = [NSMutableArray array];
    _subdataSource = [NSMutableArray array];
    
    _currentselectindex = 0;
    
    self.title = @"我的授权";
    
    [self setUpMainInterface];
    
    [self getaddata];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)getaddata{
    
    NSArray *array = @[@"page1",@"@page2",@"page3"];
    int x = arc4random() % 3;
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    [param setValue:array[x] forKey:@"code"];
    
    [MQRequestTools POSTWithMethod:adv_index params:param success:^(id json) {
        
        NSDictionary *dic = [json valueForKey:@"data"];
        _admodels = [MQAdmodels mj_objectWithKeyValues:dic];
        if (_admodels.advId) {
            [_adviews.adimageviews sd_setImageWithURL:[NSURL URLWithString:_admodels.pic] placeholderImage:[UIImage imageNamed:@""]];
        }else{
            _adviews.bst_height = 0;
            _myaruthtableview.bst_y = MAX_Y(_titleview.frame);
            _myaruthtableview.bst_height =BSTScreenH -MAX_Y(_titleview.frame);
        }
        
        
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [_adviews addGestureRecognizer:tapGesturRecognizer];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(void)tapAction:(UITapGestureRecognizer *)obj{
    NSLog(@"%@",_admodels.url);
    MQAdDetailViewController *advc = [[MQAdDetailViewController alloc] init];
    advc.url = _admodels.url;
     advc.type = _admodels.type;
    [self.navigationController pushViewController:advc animated:YES];
}

-(void)setUpMainInterface{
    
    
    self.view.backgroundColor = BSTColorFromRGB(0xefefef);
    
    MQAruthTitleview *titleview = [MQAruthTitleview mqMyaruthtitleview];
    titleview.bst_x = 0;
    titleview.bst_y = NAVHEIGHT;
    titleview.bst_width = BSTScreenW;
    titleview.bst_height = 50;

    _titleview.myaruthView.hidden = NO;
    [titleview.myAruthButton addTarget:self action:@selector(myAruthButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [titleview.waitButton addTarget:self action:@selector(waitButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [titleview.mylastButton addTarget:self action:@selector(mylastButtonPressed:)  forControlEvents:UIControlEventTouchUpInside];
    _titleview = titleview;
    _titleview.waitButton.selected = NO;
    _titleview.mylastButton.selected = NO;
    _titleview.myAruthButton.selected = YES;
    _titleview.waitview.hidden = YES;
    _titleview.mylastview.hidden = YES;
    [self.view addSubview:titleview];
    
    
    _adviews = [MQadviews loadadviews];
    _adviews.bst_x = 0;
    _adviews.bst_width = BSTScreenW;
    _adviews.bst_height = 70 *Persion;
    _adviews.bst_y = NAVHEIGHT + 50 +5;
    [self.view addSubview:_adviews];
    
    
    [self.view addSubview:self.myaruthtableview];
    
}

-(void)myAruthButtonPressed:(UIButton *)sender{
   
    if (_currentselectindex ==0) {
        return;
    }else{
        _titleview.waitButton.selected = NO;
        _titleview.mylastButton.selected = NO;
        _titleview.myAruthButton.selected = YES;
        _titleview.waitview.hidden = YES;
        _titleview.mylastview.hidden = YES;
        _titleview.myaruthView.hidden = NO;
        _currentselectindex = 0;
        [_myaruthtableview.mj_header beginRefreshing];
    }
    
}

-(void)waitButtonPressed:(UIButton *)sender{
    
    
    if (_currentselectindex ==1) {
        return;
    }else{
        _titleview.waitButton.selected = YES;
        _titleview.mylastButton.selected = NO;
        _titleview.myAruthButton.selected = NO;
        _titleview.waitview.hidden = NO;
        _titleview.mylastview.hidden = YES;
        _titleview.myaruthView.hidden = YES;
        _currentselectindex = 1;
        [_myaruthtableview.mj_header beginRefreshing];
    }
    
    
}

-(void)mylastButtonPressed:(UIButton *)sender{
    
    
    if (_currentselectindex ==2) {
        return;
    }else{
        
        _titleview.waitButton.selected = NO;
        _titleview.mylastButton.selected = YES;
        _titleview.myAruthButton.selected = NO;
        _titleview.waitview.hidden = YES;
        _titleview.mylastview.hidden = NO;
        _titleview.myaruthView.hidden = YES;
        _currentselectindex =2;
        [_myaruthtableview.mj_header beginRefreshing];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    return 10;
    
    if (_currentselectindex == 0) {
        return _dataSource.count;
    }else if (_currentselectindex ==1){
        return _waitdatasource.count;
    }else{
        return _subdataSource.count;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (_currentselectindex ==1) {
        return 151;
    }else if(_currentselectindex ==2){
        return 79;
    }else{
        return 127;
    }
    
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MQAgentAruthViewController *angentvc = [[MQAgentAruthViewController alloc] init];
    MQMyAgentListModel *model;
    if (_currentselectindex == 0) {
         model = _dataSource[indexPath.row];
        angentvc.hasButton = NO;
        
    }else if (_currentselectindex ==1){
         model = _waitdatasource[indexPath.row];
         angentvc.hasButton = YES;
        
    }else{
        model = _subdataSource[indexPath.row];
        angentvc.hasButton = NO;
    }
    angentvc.model = model;
    [self.navigationController pushViewController:angentvc animated:YES];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MQMyAruthCell *cell = [MQMyAruthCell cellWIthTableview:tableView];
    MQAruthTableViewCell *cells = [MQAruthTableViewCell cellWIthTableview:tableView];
    MQmyauthTableViewCell *cellss = [MQmyauthTableViewCell cellWIthTableview:tableView];
    MQMyAgentListModel *model;
    if (_currentselectindex == 0) {
        model = _dataSource[indexPath.row];
        if (model.parentAgent) {
            cellss.shangji.text = [NSString stringWithFormat:@"上级: %@",model.parentAgent.telephone];
        }else{
            cellss.shangji.text = [NSString stringWithFormat:@"上级: 团队管理员"];
        }
        
        if (model.productName) {
            cellss.currentname.text = [NSString stringWithFormat:@"产品名称: %@",model.productName];
        }else{
            cellss.currentname.text = [NSString stringWithFormat:@"产品名称: %@",model.seriesName];
        }
        cellss.level.text = [NSString stringWithFormat:@"代理等级：%@",model.level];
          cellss.code.text = [NSString stringWithFormat:@"产品码： %@",model.code];
        switch ([model.status integerValue]) {
            case 0:
                cellss.status.text = [NSString stringWithFormat:@"状态：待上级审核"];
                break;
                
            case 1:
                cellss.status.text = [NSString stringWithFormat:@"状态：等待管理员审核"];
                break;
            case 2:
                cellss.status.text = [NSString stringWithFormat:@"状态：审核通过，正常运行"];
                break;
            case 3:
                cellss.status.text = [NSString stringWithFormat:@"状态：审核不通过"];
                break;
            case 4:
                cellss.status.text = [NSString stringWithFormat:@"状态：停用"];
                break;
            default:
                break;
        }

        return cellss;
    }else if (_currentselectindex ==1){
           model = _waitdatasource[indexPath.row];
        if (model.parentAgent) {
            cells.shangji.text  = [NSString stringWithFormat:@"上级: %@",model.parentAgent.telephone];
        }else{
            cells.shangji.text  = [NSString stringWithFormat:@"上级微信号: 团队管理员"];
        }
        
        cells.code.text = [NSString stringWithFormat:@"产品码： %@",model.code];
     
        
        
        if (model.productName) {
              cells.level.text = [NSString stringWithFormat:@"产品名称: %@",model.productName];
        }else{
               cells.level.text = [NSString stringWithFormat:@"产品名称: %@",model.seriesName];
        }
        cells.currentname.text = [NSString stringWithFormat:@"代理等级：%@",model.level];
        cells.status.text =[NSString stringWithFormat:@"当前审核人: %@",model.verifyAgent.telephone];
        switch ([model.status integerValue]) {
            case 0:
                cells.lastlabel.text = [NSString stringWithFormat:@"状态：待上级审核"];
                break;
                
            case 1:
                cells.lastlabel.text = [NSString stringWithFormat:@"状态：等待管理员审核"];
                break;
            case 2:
                cells.lastlabel.text = [NSString stringWithFormat:@"状态：审核通过，正常运行"];
                break;
            case 3:
                cells.lastlabel.text = [NSString stringWithFormat:@"状态：审核不通过"];
                break;
            case 4:
                cells.lastlabel.text = [NSString stringWithFormat:@"状态：停用"];
                break;
            default:
                break;
        }
        
        return cells;
     
    }else{
        model = _subdataSource[indexPath.row];
    
        cell.weixinLabel.text = [NSString stringWithFormat:@"姓名: %@",model.subAgent.realName];
        cell.productcodeLabel.text = [NSString stringWithFormat:@"联系方式：%@",model.subAgent.telephone];
        cell.stateLabel.text = [NSString stringWithFormat:@"代理等级：%@",model.level];
        
        return cell;
    }

    
}

-(void)dealviews{
    
}

-(UITableView *)myaruthtableview{
    if (!_myaruthtableview) {
        
        
        _myaruthtableview = [[UITableView alloc] initWithFrame:CGRectMake(0, MAX_Y(_titleview.frame) + 10 + 70 *Persion, BSTScreenW, BSTScreenH - MAX_Y(_titleview.frame) - 10 - 70 *Persion) style:UITableViewStylePlain];
        _myaruthtableview.delegate = self;
        _myaruthtableview.dataSource = self;
        _myaruthtableview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _myaruthtableview.showsVerticalScrollIndicator = NO;
        UIView *footerview = [UIView new];
        _myaruthtableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        footerview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _myaruthtableview.tableFooterView = footerview;
        _myaruthtableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getdata];
        }];
        
        [_myaruthtableview.mj_header beginRefreshing];
        
    }
    return _myaruthtableview;
}

-(void)getdata{
    NSString *url = @"";
    

    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:@"1" forKey:@"page"];
    [param setValue:@"20" forKey:@"limit"];
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    if (_currentselectindex == 0) {
        url = agent_myAgent;
    }else if (_currentselectindex ==1){
        url = agent_waitVerify;
    }else{
        url = agent_subAgent;
    }
    
    [MQRequestTools POSTWithMethod:url params:param success:^(id json) {
        
        NSArray *array = [json valueForKey:@"data"];
        
        if (_currentselectindex == 0) {
            [_dataSource removeAllObjects];
            for (NSDictionary *dic in array) {
                MQMyAgentListModel *model = [MQMyAgentListModel mj_objectWithKeyValues:dic];
                [_dataSource addObject:model];
            }
            
            
        }else if (_currentselectindex ==1){
            [_waitdatasource removeAllObjects];
            for (NSDictionary *dic in array) {
                MQMyAgentListModel *model = [MQMyAgentListModel mj_objectWithKeyValues:dic];
                [_waitdatasource addObject:model];
            }
        }else{
            
            
            [_subdataSource removeAllObjects];
            for (NSDictionary *dic in array) {
                MQMyAgentListModel *model = [MQMyAgentListModel mj_objectWithKeyValues:dic];
                [_subdataSource addObject:model];
            }
        }
        [_myaruthtableview reloadData];
        [_myaruthtableview.mj_header endRefreshing];
        
    } failure:^(NSError *error) {
        
    }];
    
    
    
    
    
    
}

@end
